package concurrent.threadpool;

import java.util.concurrent.*;

public class MyThreadPoolExecutorDemo {

	public static void doSomething(ExecutorService executorService, int numOfRequest) {

		try {

			System.out.println(((ThreadPoolExecutor) executorService).getRejectedExecutionHandler().getClass() + ":");
			TimeUnit.SECONDS.sleep(1);

			for (int i = 0; i < numOfRequest; i++) {
				final int tempInt = i;
				executorService.execute(() -> {
					System.out.println(Thread.currentThread().getName() + "\t 给用户:" + tempInt +
							" " +
							"办理业务");
				});
			}

			TimeUnit.SECONDS.sleep(1);
			System.out.println("\n\n");

		} catch (Exception e) {
			System.err.println(e);
		} finally {
			executorService.shutdown();
		}
	}

	public static ExecutorService newMyThreadPoolExecutor(int corePoolSize,
	                                                      int maximumPoolSize,
	                                                      int blockingQueueSize,
	                                                      RejectedExecutionHandler handler) {
		return new ThreadPoolExecutor(
				corePoolSize,
				maximumPoolSize,
				1,//keepAliveTime
				TimeUnit.SECONDS,
				new LinkedBlockingQueue<>(blockingQueueSize),
				Executors.defaultThreadFactory(),
				handler);
	}


	public static void main(String[] args) {
		doSomething(newMyThreadPoolExecutor(2, 5, 3, new ThreadPoolExecutor.AbortPolicy()), 10);
		doSomething(newMyThreadPoolExecutor(2, 5, 3, new ThreadPoolExecutor.CallerRunsPolicy()),
				20);
		doSomething(newMyThreadPoolExecutor(2, 5, 3, new ThreadPoolExecutor.DiscardOldestPolicy())
				, 10);
		doSomething(newMyThreadPoolExecutor(2, 5, 3, new ThreadPoolExecutor.DiscardPolicy()), 10);
	}

}